package com.bocai.kelalacar.ui.car.set.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.OptionalSeatsBean;
import com.bocai.kelalacar.bean.UploadImgBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.set.contract.AddCarContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/27.
 */

public class AddCarPresenter extends AddCarContract.presenter {

    private final AddCarContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public AddCarPresenter(AddCarContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void addCar(String memberId, String CarLicense, String BuyTime, String Seats, String CarImageIds) {
        view.showLoading("");
        api.addCar(memberId,CarLicense,BuyTime,Seats,CarImageIds).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                    view.addCarSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void uploadImg(List<String> photos) {
        view.showLoading("正在上传图片");
        List<MultipartBody.Part> parts = new ArrayList<>();
        MultipartBody.Builder builder=new MultipartBody.Builder();
        Map<String, RequestBody> map = new HashMap<>();
        for (int i = 0;i < photos.size();i++){
            File file = new File(photos.get(i));
            map.put("file"+"\";filename =\""+ file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"),file));
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
            builder.addFormDataPart("file",file.getName(),requestBody);
            parts.add(builder.build().part(i));
        }
        api.uploadImage(parts).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
                    Log.e("result",result);
                    UploadImgBean uploadImgBean = new Gson().fromJson(result,UploadImgBean.class);
                    view.uploadImgSuccess(uploadImgBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
    @Override
    public void optionalSeats() {
        view.showLoading("");
        api.optionalSeats().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<OptionalSeatsBean>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(OptionalSeatsBean baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    view.optionalSeatsList(baseResponse.getData());
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getCarDriverList(String memberId) {
        api.getCarDriverList(memberId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
                    Log.e("result",result);
                    CarDriverBean bean = new Gson().fromJson(result,CarDriverBean.class);
                    view.getCarList(bean);
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void deleteCar(String carId, final int index) {
        view.showLoading("");
        api.deleteCar(carId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")) {
                   view.deleteCarSuccess(index);
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void modifyCar(String carId, String CarLicense, String BuyTime, String Seats, String CarImageIds) {
        view.showLoading("");
        api.modifyCar(carId,CarLicense,BuyTime,Seats,CarImageIds).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    view.modifyCarSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
